//
//  RCChangePointDetailViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-11-24.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCChangePointDetailViewController.h"
#import "RCChangePointsDetailDataModel.h"
#import "IManager+ModuleChangePointsDetail.h"
#import "MBProgressHUD.h"
#import "RCChangePointsDetailCell.h"
#import "NSArray+EX.h"
#import "RCChangeReviewDetailViewController.h"
#import "RCChangeRecordDetailViewController.h"
#import "GloableEngineService.h"
#import "RCChangeOrderViewController.h"

#define kTableViewSectionNum      4

@interface RCChangePointDetailViewController ()<IManagerDelegate,MBProgressHUDDelegate,UITableViewDataSource,UITableViewDelegate,RCChangePointsDetailDelegate,RCChangeOrderCommitDelegate>

@property(nonatomic,retain)UITableView *detailTabelView;
@property(nonatomic,retain)RCChangePointsDetailDataModel *detailDataModel;
@property(nonatomic,assign)int64_t requestID;
@property(nonatomic, retain) MBProgressHUDEx *progressHub;
@property(nonatomic,retain) NSString *goodsID;

@end

@implementation RCChangePointDetailViewController

@synthesize detailTabelView = _detailTabelView;
@synthesize detailDataModel = _detailDataModel;
@synthesize requestID = _requestID;
@synthesize progressHub = _progressHub;
@synthesize goodsID = _goodsID;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)initWithGoodsID:(NSString *)goodsID{
    self = [super init];
    if (self) {
        self.goodsID = goodsID;
    }
    return self;
}

-(void)dealloc{
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    self.detailTabelView.delegate = nil;
    self.detailTabelView.dataSource = nil;
    self.detailTabelView = nil;
    self.detailDataModel = nil;
    self.goodsID = nil;
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.titleLabelText = @"积分商城详情页";
    self.navigationItem.titleView = [self createTitleView];
    
    
    CGRect rect = self.viewContainer.frame;
    rect.size.height -=44;
    _detailTabelView = [[UITableView alloc] initWithFrame:rect];
    _detailTabelView.backgroundColor = [UIColor clearColor];
    [self.viewContainer addSubview:_detailTabelView];
    _detailTabelView.delegate = self;
    _detailTabelView.dataSource = self;
    _detailTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self refreshData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
}


#pragma
#pragma mark --- UITableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return kTableViewSectionNum;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return (self.detailDataModel != nil && self.detailDataModel.picExtraArray != nil) ? [self.detailDataModel.picExtraArray count ]:0;
        case 2:
            return (self.detailDataModel != nil && self.detailDataModel.reviewItem != nil) ? 1:0;
        case 3:
            return (self.detailDataModel != nil && self.detailDataModel.changeRecordItem != nil) ? 1:0;
            break;
            
        default:
            return 0;
            break;
    }
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    NSInteger section = [indexPath section];
    
    switch (section) {
        case 0:
            return [RCChangePointsDetailPicHeaderCell getCellHeight:self.detailDataModel];
            break;
        case 1:{
            RCChangePointsExtraItem *item = [self.detailDataModel.picExtraArray safeObjectAtIndex:row];
            return [RCChangePointsDetailPictureCell getCellHeight:item];
            break;
        }
            
        case 2:
            return [RCChangePointsDetailReviewCell getCellHeight:self.detailDataModel.reviewItem];
            break;
        case 3:{
            return [RCChangePointsDetailChangeRecordCell getCellHeight:self.detailDataModel.changeRecordItem];
            break;
        }
        
        default:
            return 0.0f;
            break;
    }
}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    NSInteger section = [indexPath section];
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d",section,row];
    if (section == 1) {
        CellIdentifier = @"ContentCellImage";
    }
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell) {
        switch (section) {
            case 0:
                cell = [[[RCChangePointsDetailPicHeaderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
                ((RCChangePointsDetailPicHeaderCell *)cell).delegate = self;
                break;
            case 1:
                cell = [[[RCChangePointsDetailPictureCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
                break;
            case 2:
                cell = [[[RCChangePointsDetailReviewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
                
                break;
            case 3:
                cell = [[[RCChangePointsDetailChangeRecordCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
                
                break;
        
            default:
                break;
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    switch (section) {
        case 0:
            
            [((RCChangePointsDetailPicHeaderCell *)cell) bindData:self.detailDataModel];
            break;
        case 1:{
            RCChangePointsExtraItem *item = [self.detailDataModel.picExtraArray safeObjectAtIndex:row];
            
            [((RCChangePointsDetailPictureCell *)cell) bindData:item];
            break;
        }
            
        case 2:
            [((RCChangePointsDetailReviewCell *)cell) bindData:self.detailDataModel.reviewItem];
            break;
        case 3:
            [((RCChangePointsDetailChangeRecordCell *)cell) bindData:self.detailDataModel.changeRecordItem];
            break;
    
        default:
            break;
    }
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSInteger section = [indexPath section];
    if (section == 2) {
        
        RCChangeReviewDetailViewController *vc = [[RCChangeReviewDetailViewController alloc]initWithShopId:self.detailDataModel.shopID withShopName:self.detailDataModel.title];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }else if (section == 3){
        RCChangeRecordDetailViewController *vc = [[RCChangeRecordDetailViewController alloc]initWithShopId:self.detailDataModel.shopID withShopName:self.detailDataModel.title];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }
    
}
#pragma
#pragma mark ServerData ----下拉刷新数据

-(void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _requestID = [[IManager sharedInstance]requestChangePointsDetailInfo:self.goodsID withDelegate:self];
}



#pragma
#pragma mark IManage Delegate
-(void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        self.detailDataModel = (RCChangePointsDetailDataModel *)dataModel;
        
        
        
        [_detailTabelView reloadData];
    }
    
    
}

-(void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }
}

-(void)gotoChangeOrderPage{
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    
    RCChangeOrderViewController *vc = [[RCChangeOrderViewController alloc]initWithGoodsID:self.goodsID];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

-(void)orderCommitSuccess{
    [self refreshData];
}

@end
